<?php
/**
 *
 * @author 罗融春
 *        
 */
class MySqlDao {
	private $conn;
	function __construct() {
		$filename = dirname ( __FILE__ ) . "/dbConfig.ini";
		$dbConfig = parse_ini_file ( $filename, true );
		
		$dsn = $dbConfig ["mysql"] ["dsn"];
		$username = $dbConfig ["mysql"] ["username"];
		$passwd = $dbConfig ["mysql"] ["passwd"];
		
		$this->conn = new \PDO ( $dsn, $username, $passwd );
	}
	
	/**
	 * 插入一条记录到$table
	 *
	 * @param String $table        	
	 * @param Array $params        	
	 */
	function insert($table, $params) {
		$sql = "";
		$field = "";
		$value = "";
		
		while ( list ( $key, $val ) = each ( $params ) ) {
			$field .= "$key,";
			$value .= "'$val',";
		}
		
		$field = substr ( $field, 0, - 1 );
		$value = substr ( $value, 0, - 1 );
		
		$sql = "insert into $table($field) values($value)";
		
		$this->conn->exec ( $sql );
	}
	
	/**
	 * 根据条件删除$table记录
	 *
	 * @param String $table        	
	 * @param Array $params        	
	 */
	function delete($table, $params = null) {
		if (null == $params)
			return;
		
		$sql = "";
		$condition = "";
		
		while ( list ( $key, $val ) = each ( $params ) )
			$condition .= "$key='$val' and";
		
		$condition = substr ( $condition, 0, - 4 );
		
		$sql = "delete from $table where $condition";
		
		$this->conn->exec ( $sql );
	}
	
	/**
	 * 删除$table所有记录
	 *
	 * @param String $table        	
	 */
	function deleteAll($table) {
		$sql = "delete from $table";
		
		$this->conn->exec ( $sql );
	}
	
	/**
	 * 查询$table记录总数
	 *
	 * @param string $table        	
	 * @param array $params        	
	 */
	function selectCount($table, $params = null) {
		$sql = "select count(*) from $table where 1=1";
		
		if (null != $params) {
			while ( list ( $key, $val ) = each ( $params ) )
				$sql .= " and $key='$val'";
		}
		
		$stmt = $this->conn->query ( $sql );
		$count = $stmt->fetchColumn ();
		
		return $count;
	}
	
	/**
	 * 从$table查询一条记录
	 *
	 * @param string $table        	
	 * @param array $fileds        	
	 * @param array $params        	
	 * @return object
	 */
	function selectOne($table, $fileds, $params = null) {
		$sql = $this->buildSql ( $table, $fields, $params, null, null );
		
		$stmt = $this->conn->query ( $sql );
		$resSet = $stmt->fetchAll ();
		
		return $resSet;
	}
	
	/**
	 * 从$table查询一批记录
	 *
	 * @param String $table        	
	 * @param Array $params        	
	 */
	function selectList($table, $fields = null, $params = null, $orderBy = null, $page = null) {
		$sql = $this->buildSql ( $table, $fields, $params, $orderBy, $page );
		
		$stmt = $this->conn->query ( $sql );
		$resSet = $stmt->fetchAll ();
		
		return $resSet;
	}
	
	/**
	 * 从$table查询一页
	 *
	 * @param string $table        	
	 * @param array $fields        	
	 * @param array $params        	
	 * @param array $orderBy        	
	 * @param array $page        	
	 * @return array
	 */
	function selectPage($table, $fields = null, $params = null, $orderBy = null, $page = null) {
		$total = $this->selectCount ( $table, $params );
		$pageData = $this->selectList ( $table, $fields, $params, $orderBy, $page );
		
		$page->total = $total;
		$page->pageCount = $page->total / $page->pageSize + (0 == $page->total % $page->pageSize ? 0 : 1);
		$page->pageData = $pageData;
		
		return $page;
	}
	
	/**
	 * 生成查询SQL
	 *
	 * @param string $table
	 *        	查询表
	 * @param array $fields
	 *        	查询字段
	 * @param array $params
	 *        	查询参数
	 * @param array $orderBy
	 *        	Order By关键字集合
	 * @param array $page
	 *        	分页对象
	 * @return string 查询SQL
	 */
	function buildSql($table, $fields = null, $params = null, $orderBy = null, $page = null) {
		$sql = "";
		$strFields = "";
		$strParams = "1=1";
		$strOrderBy = "";
		$limit = "";
		
		if (null == $fields)
			$strFields = "*";
		else {
			for($i = 0; $i < count ( $fileds ); $i ++)
				$strFields .= "$fileds[i],";
			$strFields = substr ( $strFields, 0, - 1 );
		}
		
		if (null != $params)
			while ( list ( $key, $val ) = each ( $params ) )
				$strParams .= " and $key='$val'";
		
		if (null != $orderBy) {
			for($i = 0; $i < count ( $orderBy ); $i ++)
				$strOrderBy .= "$orderBy[$i],";
			$strOrderBy = substr ( $strOrderBy, 0, - 1 );
		}
		
		if (null != $page)
			$limit = "$page->limitStart,$page->limitSize";
		
		if (empty ( $strOrderBy ))
			$sql = "select $strFields from $table where $strParams limit $limit";
		else
			$sql = "select $strFields from $table where $strParams order by $strOrderBy limit $limit";
		
		return $sql;
	}
}

/**
 * MySQL分页
 *
 * @author 罗融春
 *        
 */
class MySqlPage {
	/**
	 * 页码
	 *
	 * @var int
	 */
	public $pageIndex = 0;
	/**
	 * 总页数
	 *
	 * @var int
	 */
	public $pageCount;
	/**
	 * 页大小
	 *
	 * @var int
	 */
	public $pageSize = 10;
	/**
	 * 页数据
	 *
	 * @var object/array
	 */
	public $pageData;
	
	/**
	 * 总记录数
	 *
	 * @var int
	 */
	public $total;
	
	/**
	 * 分页查询参数
	 *
	 * @var array
	 */
	public $queryParams;
	
	/**
	 *
	 * @var int
	 */
	public $limitStart;
	
	/**
	 *
	 * @var int
	 */
	public $limitSize;
	
	/**
	 * 构造函数
	 *
	 * @param int $pageIndex        	
	 * @param int $pageSize        	
	 * @param array $queryParams        	
	 */
	function MySqlPage($pageIndex = 1, $pageSize = 10, $queryParams) {
		$this->pageIndex = $pageIndex;
		$this->pageSize = $pageSize;
		$this->queryParams = $queryParams;
		$this->limitStart = (0 >= $pageIndex ? 0 : $pageIndex - 1) * $pageSize;
		$this->limitSize = $pageSize;
	}
}
?>